from sqlalchemy.orm import Session
from app.models.item_model import Item


def create_item(name: str, description: str, db: Session):
    new_item = Item(name=name, description=description)
    db.add(new_item)
    db.commit()
    db.refresh(new_item)
    return new_item


def get_item(item_id: int, db: Session):
    item = db.query(Item).filter(Item.id == item_id).first()
    return item


def update_item(item_id: int, name: str, description: str, db: Session):
    item = db.query(Item).filter(Item.id == item_id).first()
    if item:
        item.name = name
        item.description = description
        db.commit()
        db.refresh(item)
    return item


def delete_item(item_id: int, db: Session):
    item = db.query(Item).filter(Item.id == item_id).first()
    if item:
        db.delete(item)
        db.commit()
        return True
    return False
    